//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _USERS_TAB_YG_H__
#define _USERS_TAB_YG_H__

#include "def.h"
#include "logctrl.h"
#include "searchimpl.h"
#include "listpaircontroller.h"

class CUsersTab: public wxPanel, public IGUIBind, public CSearchImpl<CUsersTab>, public ILoggable
{
public:
								CUsersTab( class wxWindow* pParent );
	virtual						~CUsersTab();

	//IGUIBind
	virtual void				OnResponse(const COMMANDRESPONSE& resp);

	//ISearchProvider
	virtual void 				GetSearchFields(SEARCHFIELD*& sf, size_t& sz);
	virtual cSearchOwnerID		GetID()				{ return cSearchOwnerIDRtrUsers; }
	virtual void				OnFilterChanged();

	bool						OnDropText(wxCoord x, wxCoord y, const wxString& data) { return false; }
	void						OnDragDropFinished( wxWindowID id, wxDragResult result) {}


	//List Control
	void						OnContextMenu(long id, long col) {}

	long						GetItemCount(wxWindowID id=wxID_ANY)
	{
		return m_storage.size();
	}

	wxString					GetItemText(long item, long column, wxWindowID id=wxID_ANY);

	int							GetItemColumnImage(long item, long column, wxWindowID id=wxID_ANY);

	virtual int					GetItemImage(long item, wxWindowID id=wxID_ANY)
	{
		return -1;
	}

	virtual wxString			GetLog();

protected:
	void						OnColumn(wxListEvent& ev);
	void						OnMouseEvent(wxMouseEvent& event);
	DECLARE_EVENT_TABLE()

private:
	enum cRouterUserType		{ cNotDetectedUser, cEthernetUser,	 cWirelessUser };
	enum cRouterUserStatus		{ cRouterUserOnline, cRouterUserOffline, cRouterUserNew };
	enum cRouterMacBlockStatus	{ cRouterMacBlockNone, cRouterMacBlocWhiteList, cRouterMacBlocBlackList  };
	enum cRouterSchedStatus		{ cRouterSchedNone, cRouterSchedOn, cRouterSchedBlocked  };

	typedef struct tagROUTERUSER
	{
		tagROUTERUSER():bFromMacBlock(false),
			mbstatus(cRouterMacBlockNone),
			schedstatus(cRouterSchedNone),
			lLast(0)
		{}

		bool				bFromMacBlock;

		wxString			strIP;
		wxString			strMAC;
		cRouterUserType		type;
		cRouterUserStatus	status;
		time_t				lLast;

		//scheduler
		cRouterMacBlockStatus mbstatus;
		cRouterSchedStatus	schedstatus;
		
	}ROUTERUSER;
	typedef std::vector<ROUTERUSER> ROUTERUSERS;

	void						OnArpTable( const char* szXML );
	void						AddMacBlockData();
	void						AddMacBlockData(const MACBLOCK& block);
	void						RemoveMACBlock();
	void						FillBlockDetails(ROUTERUSER& user, const MACENTRY& entry);

	void						ProcessData();
	void						DoFilter();
	void						DoSort();


	wxString					FieldToString( int column, const ROUTERUSER& entry);
	wxString					GenerateType(cRouterUserType		type);
	wxString					GenerateStatus(const ROUTERUSER& entry);
	wxString					GenerateAccessStatus(const ROUTERUSER& entry);
	wxString					GenerateSchedulerStatus(const ROUTERUSER& entry);


	struct CSortHelper
	{
		CSortHelper(int col, bool bSortAsc):m_col(col), m_bSortAsc(bSortAsc) {}

		bool operator() (const ROUTERUSER& a, const ROUTERUSER& b);

	private:
		int			 m_col;
		bool		 m_bSortAsc;
	};


	void						UpdateConnectedClient(const wxString& strIP, const wxString& strMAC, bool bForceWireless=false);


private:
	class CSearchPanel*			m_pSearch;
	bool						m_bSortAsc;
	class wxListCtrl*			m_pList;
	ROUTERUSERS					m_storage;
	ROUTERUSERS					m_entriesBeforeFilter;
	unsigned int				m_nSortCol;
	//PROCTABLEWITHHEADER			m_tableWireless;

	class CPersistenList*		m_pWhiteList;
	class CPersistenList*		m_pBlackList;
	CListPairController*		m_pController;
	class wxImageList*			m_pImageList;
};

#endif //_USERS_TAB_YG_H__